<?php
class Merchant_Model_ProductAttached
{
	protected $_table;

	public function __construct(){
		$this->_table = new Merchant_Model_DbTable_ProductAttached();
	}

	public function getDbTable(){
		return $this->_table;
	}

    public function add($row)
    { 
		return $this->_table->insert($row); 
    }
    
	public static function getInstance(){
		return new Merchant_Model_ProductAttached();
	}

	/**
	* 通过产品id查找图片
	* @param $productId
	* @return Zend_db_table_rowset
	*/
	public function getAllByProductId($productId){
		$where = $this->_table->select()->where("product_id = ?",$productId);
		$rowset = $this->_table->fetchAll($where,"pa_sort asc");
		return $rowset;
	}

	/**
	* 通过产品id查找图片
	* @param $productId
	* @return Zend_db_table_rowset
	*/
	public function getPicturesByPid($productId){
		$where = $this->_table->select()->where("product_id = ?",$productId);
		$res = $this->_table->fetchAll($where,"pa_sort asc");
		return $res;
	}
	
	public function getByProductId($productId){
		$where = $this->_table->select()->where("product_id = ?",$productId)->where("pa_file_type=?","img");
		$rowset = $this->_table->fetchAll($where,"pa_sort asc");
		return $rowset;
	}

	
	
	/**
	* 通过产品id查找links
	* @param $productId
	* @return Zend_db_table_rowset
	*/
	public function getLinksByProductId($productId){
		$where = $this->_table->select()->where("product_id = ?",$productId)->where("pa_file_type=?","link");
		$rowset = $this->_table->fetchAll($where,"pa_sort asc");
		return $rowset;
	}
	/**
	* 通过产品id删除图片
	* @param $productId
	* @return int
	*/
	public function deleteByProductId($productId){
		$where = $this->_table->getAdapter()->quoteInto("product_id = ?",$productId);
		$where .= $this->_table->getAdapter()->quoteInto(" and pa_file_type=?","img");
		return $this->_table->delete($where);
	}
	/**
	* 通过产品id删除links
	* @param $productId
	* @return int
	*/
	public function deleteLinksByProductId($productId){
		$where = $this->_table->getAdapter()->quoteInto("product_id = ?",$productId);
		$where .= $this->_table->getAdapter()->quoteInto(" and pa_file_type=?","link");
		return $this->_table->delete($where);
	}
	
	/**
	* 获得产品展示图片
	*
	*/
	public function getViewPicture($productId){
		$where = $this->_table->select()->where("product_id = ?",$productId)->where("(pa_file_type=?","img")->orWhere("pa_file_type=?)","link");
		$rowset = $this->_table->fetchRow($where,"pa_sort asc");
		return $rowset;
	}
}

